#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
    int s=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s;
}
const int MAXN=3e5+5,INF=1e18;
struct number{
    int a,b;
}n[MAXN];
int g[MAXN];
bool cmp_sort(number x,number y){
    return x.b<y.b;
}
struct ST1{
    int st[MAXN][20];
    void build(int N){
        for(int i=1;i<=N;++i) st[i][0]=n[i].a;
        for(int j=1;j<=19;++j)  
            for(int i=1;i+(1<<j)-1<=N;++i)  
                st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
    }
    int query(int x,int y){
        int len=g[y-x+1];
        return min(st[x][len],st[y-(1<<len)+1][len]);
    }
}st1;
struct Pair{
    int maxn1,maxn2;
    Pair(int x=0,int y=0):maxn1(x),maxn2(y){}
}stt[MAXN][20];
struct ST2{
    Pair cmp(Pair x,Pair y){
        if(x.maxn2<y.maxn2) return Pair(min(x.maxn1,y.maxn2),x.maxn2);
        else return Pair(min(x.maxn2,y.maxn1),y.maxn2);
    }
    void build(int N){
        for(int i=1;i<=N;++i) stt[i][0]=Pair(INF,n[i].a+n[i].b);
        for(int j=1;j<=19;++j)
            for(int i=1;i+(1<<j)-1<=N;++i)  
                stt[i][j]=cmp(stt[i][j-1],stt[i+(1<<j-1)][j-1]);
    }
    Pair query(int x,int y){
        int len=g[y-x+1];
        return cmp(stt[x][len],stt[y-(1<<len)+1][len]);
    }
}st2;
void solve(int N){
    int ans=INF;
    // printf("%lld\n",N);
    for(int i=2;i+1<=N;++i){
        int pre=st1.query(1,i-1);
        Pair p=st2.query(i,N);
        ans=min(ans,p.maxn1+p.maxn2+pre);
    }
    printf("%lld\n",ans);
}
signed main(){
    freopen("boast.in","r",stdin);
    freopen("boast.out","w",stdout);
    int N=read();
    g[1]=0;for(int i=2;i<=N;++i) g[i]=g[i>>1]+1;
    for(int i=1;i<=N;++i) n[i].a=read(),n[i].b=read();
    sort(n+1,n+1+N,cmp_sort);
    st1.build(N),st2.build(N);
    solve(N);
    return 0;
}